const { sequelize } = require("../db/index")
const { DataTypes, Model } = require("sequelize")
const User = require("./user_model")
const Cleaner = require("./cleaner_model")
const moment = require("moment")

class Goods extends Model {
  static async hasCate(id) {
    const cate = await Cate.findOne({ where: { id } })
    if (cate === null) {
      return false
    } else {
      return true
    }
  }
  static async createCate({
    goods_name,
    full_goods_name,
    cat_id,
    attr,
    stock,
    sales,
    weight,
    desc,
    ord,
    store_id,
    is_hot,
    is_recommend,
    status,
    is_delete,
    slider_image,
    content
  }) {
    const goods = await Goods.create({
      goods_name,
      full_goods_name,
      cat_id,
      attr,
      stock,
      sales,
      weight,
      desc,
      ord,
      store_id,
      is_hot,
      is_recommend,
      status,
      is_delete,
      slider_image,
      content
    })
    return goods
  }

  static async createGoods({
    goods_name,
    full_goods_name,
    cat_id,
    attr,
    stock,
    sales,
    unit_name,
    unit_name2,
    unit_convert,
    unit_convert2,
    weight,
    desc,
    sort,
    store_id,
    is_hot,
    is_recommend,
    status,
    is_delete,
    slider_image,
    content
  }) {
    const goods = await Goods.findOrCreate({
      where: { goods_name, full_goods_name },
      default: {
        goods_name,
        full_goods_name,
        cat_id,
        attr,
        stock,
        sales,
        unit_name,
        unit_name2,
        unit_convert,
        unit_convert2,
        weight,
        desc,
        sort,
        store_id,
        is_hot,
        is_recommend,
        status,
        is_delete,
        slider_image,
        content
      }
    })
    // const goods = await Goods.create({ goods_name, full_goods_name, cat_id, attr, stock, sales, weight, desc, ord, store_id, is_hot, is_recommend, status, is_delete, slider_image, content })
    return goods
  }

  static async addAdmin(buildingId, userId) {
    try {
      const cate = await Cate.findOne({ where: { id: buildingId } })
      const user = await User.findOne({ where: { id: userId } })
      const result = await cate.addAdmin(user)
      return result
    } catch (e) {
      throw new Error("已存在该管理员，添加失败")
    }
  }
  static async addCleaner(buildingId, cleanerId) {
    try {
      const cleaner = await Cleaner.findOne({ where: { id: cleanerId } })
      cleaner.buildingId = buildingId
      return await cleaner.save()
    } catch (e) {
      throw new Error("添加保洁员失败")
    }
  }
  static async addFloor(buildingId, floor) {
    try {
      floor.buildingId = buildingId
      return await floor.save()
    } catch (error) {
      console.log(error)
    }
  }
}

Goods.init(
  {
    id: {
      comment: "商品ID",
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false
    },
    goods_name: {
      comment: "商品名称",
      type: DataTypes.STRING,
      allowNull: false
    },
    full_goods_name: {
      comment: "商品名称—简称",
      type: DataTypes.STRING,
      allowNull: false
    },
    cat_id: {
      comment: "分类ID",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    store_id: {
      comment: "分店ID",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    unit_name: {
      comment: "基本单位",
      type: DataTypes.STRING,
      allowNull: false
    },
    unit_name2: {
      comment: "辅助单位",
      type: DataTypes.STRING,
      allowNull: true
    },
    unit_convert: {
      comment: "基本单位换算",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    unit_convert2: {
      comment: "辅助单位换算",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    attr: {
      comment: "规格",
      type: DataTypes.INTEGER,
      allowNull: true
    },
    stock: {
      comment: "库存",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    sales: {
      comment: "销量",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    sort: {
      comment: "排序",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    weight: {
      comment: "商品重量",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    desc: {
      comment: "描述",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    is_hot: {
      comment: "是否热销：",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    is_recommend: {
      comment: "是否推荐：",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    status: {
      comment: "状态",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    is_delete: {
      comment: "是否删除：",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    slider_image: {
      comment: "轮播图",
      type: DataTypes.INTEGER,
      allowNull: false
    },
    content: {
      comment: "详情",
      type: DataTypes.INTEGER,
      allowNull: true
    },

    update_time: {
      comment: "更新时间",
      type: DataTypes.STRING,
      get() {
        return moment(this.getDataValue("update_time")).format(
          "YYYY-MM-DD HH:mm:ss"
        )
      }
    }
  },
  {
    sequelize,
    modelName: "sp_goods",
    freezeTableName: true,
    paranoid: false,
    timestamps: false
  }
)

module.exports = Goods
